﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EEPNetFLClient
{
    public partial class frmUpdateHistory : Form
    {
        System.Data.DataTable _dt = null;
        public frmUpdateHistory(object count)
        {
            int _count = Convert.ToInt32(count);
            InitializeComponent();
            LoadData(_count);
        }

        /// <summary>
        /// 加载更新列表
        /// </summary>
        /// <param name="top"></param>
        void LoadData(int top)
        {            
            string sUser = Srvtools.CliUtils.fLoginUser;
            _dt = Srvtools.CliUtils.CallMethod("sAdm", "GetUpdateHistory", new object[] { top, sUser })[2] as DataTable;
            System.Text.StringBuilder _sb = new StringBuilder();
            foreach (DataRow dr in _dt.Rows)
            {
                _sb.Append(dr["Desp"].ToString());
                _sb.Append("<br>");
            }
            this.webDesp.DocumentText = _sb.ToString();
        }

        /// <summary>
        /// 最近10次更新记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTen_Click(object sender, EventArgs e)
        {
            LoadData(10);
        }

        /// <summary>
        /// 全部更新记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAll_Click(object sender, EventArgs e)
        {
            LoadData(-1);
        }

        /// <summary>
        /// 窗体关闭时，保存用户看过的更新记录，以便下次不再让用户重复查看
        /// </summary>
        void SaveData()
        {
            try
            {
                DataRow[] drs = _dt.Select("Times<3");
                System.Text.StringBuilder _sbRowId = new StringBuilder();
                System.Text.StringBuilder _sbtTimes = new StringBuilder();
                string sUser = Srvtools.CliUtils.fLoginUser;
                string sNext =this.ckbNext.Checked? "1":"0";
                foreach (DataRow dr in drs)
                {
                    _sbRowId.Append(dr["RowId"].ToString() + ",");
                    _sbtTimes.Append(dr["Times"].ToString() + ",");
                }
                Srvtools.CliUtils.CallMethod("sAdm", "SetUpdateHistory",
                    new object[] { _sbRowId.ToString(), _sbtTimes.ToString(), sUser, sNext, sUser });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmUpdateHistory_FormClosed(object sender, FormClosedEventArgs e)
        {
            SaveData();
        }
    }
}
